package cn.imvc.app.controllers.seller;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import cn.imvc.app.controllers.goods.GoodsControllerImpl;
import cn.imvc.app.entities.Express;
import cn.imvc.app.entities.Seller;
import cn.imvc.app.models.ExpressModel;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.app.models.SellerModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;
/**
 * 
 * @author zhy
 * @date 2016年8月2日下午4:01:47
 *
 */
public class ExpressControllerImpl implements ExpressController {
	static Logger logger = Logger.getLogger(ExpressControllerImpl.class);
	public Page<Express> page;
	public ViewRequest viewRequest;
	public Express express;
	public int id;
	public int[] ids;
	public String search_content;
	public int sellerId;
	public Seller seller;
    private double theFirstPrice;
    private double theAdditionalPrice;
    
	@Autowired
	private ExpressModel expressModel;
	@Autowired
	private SellerModel sellerModel;
	@Autowired
	LoggingModel loggingModel;
	public String search() {
		return null;
	}

	public String index() {
		Map<String, String> searchMap = new HashMap<String, String>();
		searchMap.put("sellerId_eq_string", String.valueOf(sellerId));
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.setParams(searchMap);
		if(search_content!=null&&!search_content.equals("")){
			viewRequest.getParams().put("name_like", search_content);
			viewRequest.getParams().put("_and_seller_dot_id_eq_number", ""+sellerId);
		}
		page = expressModel.find(viewRequest);
		return "express/index";
	}

	public String show() {
		express = expressModel.get(id);
		return "express/edit";
	}

	public String editNew() {
		seller  = sellerModel.get(sellerId);
		return "express/edit";
	}

	public String create() {
		express.setFirstPrice((int)(theFirstPrice*100));
		express.setAdditionalPrice((int)(theAdditionalPrice*100));
		express.setSeller(sellerModel.get(seller.getId()));
		int result = expressModel.save(sellerId,express);
		if(result==0){
			return Render.json(new DialogBean("300","快递编码不能重复","seller_express","closeCurrent"));
		}else if(result==-1){
			return Render.json(new DialogBean("300","快递名称不能重复","seller_express","closeCurrent"));
		}
		loggingModel.info(logger,"添加快递："+express.getName());
		return Render.json(new DialogBean("200","添加成功","seller_express","closeCurrent"));
	}

	public String edit() {
		express = expressModel.get(id);
		seller = express.getSeller();
		return "express/edit";
	}

	public String update() {
		express.setFirstPrice((int)(theFirstPrice*100));
		express.setAdditionalPrice((int)(theAdditionalPrice*100));
		express.setSeller(sellerModel.get(seller.getId()));
		int result = expressModel.update(id,sellerId,express);
		if(result==0){
			return Render.json(new DialogBean("300","快递编码不能重复","seller_express","closeCurrent"));
		}else if(result==-1){
			return Render.json(new DialogBean("300","快递名称不能重复","seller_express","closeCurrent"));
		}
		loggingModel.info(logger,"修改快递："+express.getName());
		return Render.json(new DialogBean("200","修改成功","seller_express","closeCurrent"));
	}

	public String destroy() {
		String result = expressModel.delete(id);
		if(result.equals("")){
			loggingModel.info(logger,"删除快递："+id);
			return Render.json(new DialogBean("200","删除成功","seller_express"));
		}else{
			return Render.json(new DialogBean("300",result,"seller_express"));
		}
	}

	public String destroyBatch() {
		expressModel.deleteBatch(ids);
		return Render.json(new DialogBean("200","删除成功","seller_express"));
	}
	
	/**
	 * 外部调用
	 * @return
	 */
	public String dialog(){
		Map<String, String> searchMap = new HashMap<String, String>();
		searchMap.put("sellerId_eq_string", String.valueOf(sellerId));
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.setParams(searchMap);
		if(search_content!=null&&!search_content.equals("")){
			viewRequest.getParams().put("name_like", search_content);
			viewRequest.getParams().put("_and_seller_dot_id_eq_number", ""+sellerId);
		}
		page = expressModel.find(viewRequest);
		return "express/dialog";
	}
	
	public ViewRequest getViewRequest() {
		return viewRequest;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public Express getExpress() {
		return express;
	}

	public void setExpress(Express express) {
		this.express = express;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int[] getIds() {
		return ids;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public String getSearch_content() {
		return search_content;
	}

	public void setSearch_content(String search_content) {
		this.search_content = search_content;
	}

	public Seller getSeller() {
		return seller;
	}

	public void setSeller(Seller seller) {
		this.seller = seller;
	}

	public int getSellerId() {
		return sellerId;
	}

	public void setSellerId(int sellerId) {
		this.sellerId = sellerId;
	}

	public double getTheFirstPrice() {
		return theFirstPrice;
	}

	public void setTheFirstPrice(double theFirstPrice) {
		this.theFirstPrice = theFirstPrice;
	}

	public double getTheAdditionalPrice() {
		return theAdditionalPrice;
	}

	public void setTheAdditionalPrice(double theAdditionalPrice) {
		this.theAdditionalPrice = theAdditionalPrice;
	}



}
